<%@page import="login.ArticuloForm"%>
<%@page import="java.util.TreeMap"%>
<%@page import="sun.security.util.Password"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Process Login de entrada</TITLE>
<META NAME="description"
CONTENT="Procesar busqueda de un titulo y/o autor">
</HEAD>
<BODY BGCOLOR="#FDF5E6" TEXT="#000000" LINK="#0000EE"
VLINK="#551A8B" ALINK="#FF0000">


<%@ page import="java.util.Map, java.util.HashMap, login.LoginForm, login.LoginManager"
%>

<jsp:useBean id="arts" scope="request" class="login.Articulos">
  <jsp:setProperty name="arts" property="*"/>
</jsp:useBean>

<jsp:useBean id="articulo" scope="request" class="login.ArticuloForm">
  <jsp:setProperty name="articulo" property="*"/>
</jsp:useBean>

<%
int max = arts.getArticulos().size();
String title = request.getParameter("titleName");
String author = request.getParameter("authorName");
Map errors = new HashMap();
if((title == null || !(title.length()>0))&&(author == null || !(author.length()>0))){
    errors.put("search", "Both title and author are empty fields");
    TreeMap<Integer,ArticuloForm> articulos = arts.getAll();
    session.setAttribute("articlesSearch", articulos);

}
if (!errors.isEmpty()) {
  session.setAttribute("errors", errors);
%>

<jsp:forward page="MainPage.jsp" />

<% } 
else {
    TreeMap<Integer,ArticuloForm> articulos = null;
    if((title != null && title.length()>0) && (author == null || !(author.length()>0))){
        articulos = arts.getArticulosByTitle(title);
    }else if((author != null && author.length()>0) && (title == null || !(title.length()>0))){
        articulos = arts.getArticulosByAuthor(author);
    }else{
        articulos = arts.getArticulosByTitleAndAuthor(title, author);
    }
    if(articulos.isEmpty()){
        errors.put("search", "No results were found");
    }else{
        session.setAttribute("articlesSearch", articulos);
    }
    if (!errors.isEmpty()) {
        session.setAttribute("errors", errors);
    }
%>
%>
    <jsp:forward page="MainPage.jsp" />
<%}
%>

<tr>
<td width="50%"></td>
<td align="left" width="50%">
<input type="submit" value="Login">
</td>
</tr>
</table>
</form>
<UL>
</UL>
</BODY>
</HTML>